<?php
namespace App\Repository\Db;

use App\Exceptions\ValidateException;
use App\Model\Goods;
use App\Model\TaoCang;
use App\Repository\Contracts\GoodsInterface;
use Illuminate\Support\Facades\DB;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 14:28
 *
 * @Description:
 */
class GoodsRepository extends Repository implements GoodsInterface
{
    protected $rules = [
        'created' => [
            'category_id' => 'required|exists:goods_category,id',
            'article_no' => 'required|min:4|max:32',
            'bar_code' => 'required|min:6|max:32',
            'zh_name' => 'required|min:2|max:64',
            'en_name' => 'required|min:2|max:64',
            'mnemonic' => 'required|min:2|max:32',
            'brand' => 'required|min:2|max:32',
            'tax' => 'required',
            'price' => 'required',
            'inventory' => 'required|integer'
        ],
        'updated' => [
            'category_id' => 'required|exists:goods_category,id',
            'article_no' => 'required|min:4|max:32',
            'bar_code' => 'required|min:6|max:32',
            'zh_name' => 'required|min:2|max:64',
            'en_name' => 'required|min:2|max:64',
            'mnemonic' => 'required|min:2|max:32',
            'brand' => 'required|min:2|max:32',
            'tax' => 'required',
            'price' => 'required',
            'inventory' => 'required|integer'
        ]
    ];

    protected $messages = [
        'category_id.required' => "分类 ID 不能为空",
        'category_id.exists' => "分类不存在",
        'article_no.required' => "货号不能为空",
        'article_no.min' => "货号不能少于4个字",
        'article_no.max' => "货号不能多于32个字",
        'bar_code.required' => "条形码不能为空",
        'bar_code.min' => "条形码不能少于6个字",
        'bar_code.max' => "条形码不能多于32个字",
        'zh_name.required' => "中文名不能为空",
        'zh_name.min' => "中文名不能少于2个字",
        'zh_name.max' => "中文名不能多于32个字",
        'en_name.required' => "英文名不能为空",
        'en_name.min' => "英文名不能少于2个字",
        'en_name.max' => "英文名不能多于32个字",
        'mnemonic.required' => "助记码不能为空",
        'mnemonic.min' => "助记码不能少于2个字",
        'mnemonic.max' => "助记码不能多于32个字",
        'brand.required' => "品牌不能为空",
        'brand.min' => "品牌不能少于2个字",
        'brand.max' => "品牌不能多于32个字",
        'tax.required' => "税额不能为空",
        'price.required' => "金额不能为空",
        'inventory.required' => "库存不能为空",
        'inventory.integer' => "库存必须是整数",
    ];

    protected function modelName()
    {
        return 'App\Model\Goods';
    }

    public function lists($limit, $page, $map = [])
    {


        $query = $this->searchMap($map);

        $total = $query->count();
        $data = $query->orderBy('id', 'desc')->with('category')->with('taocang')->simplePaginate($limit, ['*'], 'page', $page);
        $list = [];
        foreach ($data as $key => $val) {
            $list[$key] = $val;
            $list[$key]['categoryName'] = isset($val->category)?$val->category->name:null;
            //获取套餐名称
            $title=DB::table('promotion')->where('id',$val->taocang_id,'=')->first();
            if($val->taocang_id > 0){
                $list[$key]['taocangName']  =$title['title'];
            }else{
                $list[$key]['taocangName']  ='无';
            }

        }
        return $this->simplePaginate('list', $list, $total, ceil($total/$limit), $page);
    }

    public function detail($id)
    {
        if (env('DIRECT_SALE')) {
            return $this->findBy('goods_id', $id);
        }

        return $this->findBy('id', $id);
    }

    public function detailBy($map = [])
    {
        return $this->searchMap($map, true)->first();
    }

    public function created(array $data)
    {
        if (env('DIRECT_SALE')) {
            throw new ValidateException('没有权限操作');
        }
        $this->validate($data, 'created');
        $lastId = $this->save($data);
        $lastId && $this->update(['goods_id' => $lastId], $lastId, 'id');

        return $lastId;
    }

    public function updated($id, array $data)
    {
        if (env('DIRECT_SALE')) {
            throw new ValidateException('没有权限操作');
        }
        $this->validate($data, 'updated');

        return $this->update($data, $id, 'id');
    }

    public function updateStatus($id, $status = Goods::OFF_SHELVES)
    {
        if (env('DIRECT_SALE')) {
            throw new ValidateException('没有权限操作');
        }

        return $this->model->where('id', '=', $id)->update(['status' => $status]);
    }

    public function updateInventory(array $data)
    {
        if (empty($data)) {
            throw new ValidateException('操作失败');
        }

        foreach ($data as $val) {
            $this->model->where('goods_id', '=', $val['goods_id'])->decrement('inventory', $val['counts']);
        }
    }

    public function findIdForFirst()
    {
        return $this->model->orderBy('updated_at', 'desc')->limit(1)->get()->toArray();
    }
}